﻿using OrderSystem.Common;
using OrderSystem.DataAccess;
using OrderSystem.Templet;
using System;
using System.Data;
using System.Data.SqlClient;
using System.Windows.Forms;

namespace OrderSystem
{
    public partial class FormInvoiceList : FormTemplet
    {
        private InvoiceCondition _invoiceCondition = null;

        public FormInvoiceList()
        {
            this.InitializeComponent();
        }

        public FormInvoiceList(InvoiceCondition ic) : this()
        {
            this._invoiceCondition = ic;
            this.LoadInvoiceByCondition();
        }

        public FormInvoiceList(string sProductID) : this()
        {
            DataTable dataTable = null;
            Invoices invoices = new Invoices();
            try
            {
                dataTable = invoices.GetInvoicesByProductID(sProductID);
            }
            catch (SqlException ex)
            {
                MessageBox.Show(ex.Message + "\r\n\r\n或者是连接数据库的通道出现故障，请检查！\r\n\r\n订单项信息可能没有读出来", "错误", MessageBoxButtons.OK, MessageBoxIcon.Hand);
            }
            catch (Exception ex2)
            {
                MessageBox.Show(ex2.Message + "\r\n\r\n订单信息可能没有读出来", "错误", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
            invoices.Dispose();
            if (dataTable != null)
            {
                if (dataTable.Rows.Count > 0)
                {
                    this.dgInvoices.DataSource = null;
                    this.dgInvoices.DataSource = dataTable;
                }
                else
                {
                    MessageBox.Show("根据你提供的条件，在系统中没有找到你要得信息！\r\n\r\n信息可能没有读出来", "提示！", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    this.dgInvoices.DataSource = null;
                }
                dataTable.Dispose();
            }
        }

        private void LoadInvoiceByCondition()
        {
            DataTable dataTable = null;
            Invoices invoices = new Invoices();
            try
            {
                dataTable = invoices.GetInvoicesByCondition(this._invoiceCondition);
            }
            catch (SqlException ex)
            {
                MessageBox.Show(ex.Message + "\r\n\r\n或者是连接数据库的通道出现故障，请检查！\r\n\r\n订单项信息可能没有读出来", "错误", MessageBoxButtons.OK, MessageBoxIcon.Hand);
            }
            catch (Exception ex2)
            {
                MessageBox.Show(ex2.Message + "\r\n\r\n订单信息可能没有读出来", "错误", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
            invoices.Dispose();
            if (dataTable != null)
            {
                if (dataTable.Rows.Count > 0)
                {
                    this.dgInvoices.DataSource = null;
                    this.dgInvoices.DataSource = dataTable;
                }
                else
                {
                    MessageBox.Show("根据你提供的条件，在系统中没有找到你要得信息！\r\n\r\n信息可能没有读出来", "提示！", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    this.dgInvoices.DataSource = null;
                    base.Dispose();
                }
                dataTable.Dispose();
            }
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            if (!LoginInfo.Role[PowerName.Invoice_Update])
            {
                MessageBox.Show("你没有权限操作这里！", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
            else
            {
                try
                {
                    string text = this.dgInvoices.Rows[0].Cells[0].Value.ToString();
                }
                catch (Exception)
                {
                    MessageBox.Show("表格中没有数据", "错误", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    return;
                }
                int rowIndex = this.dgInvoices.CurrentCell.RowIndex;
                FormUpdateInvoice formUpdateInvoice = new FormUpdateInvoice(this.dgInvoices.Rows[rowIndex].Cells[1].Value.ToString());
                formUpdateInvoice.MdiParent = base.MdiParent;
                formUpdateInvoice.Show();
                formUpdateInvoice.Activate();
            }
        }

        private void btnShow_Click(object sender, EventArgs e)
        {
            try
            {
                string text = this.dgInvoices.Rows[0].Cells[0].Value.ToString();
            }
            catch (Exception)
            {
                MessageBox.Show("表格中没有数据", "错误", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                return;
            }
            int rowIndex = this.dgInvoices.CurrentCell.RowIndex;
            new FormInvoiceInfo(this.dgInvoices.Rows[rowIndex].Cells[1].Value.ToString())
            {
                MdiParent = base.MdiParent
            }.Show();
        }

        private void dgInvoices_CellMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            if (e.RowIndex >= 0 && e.RowIndex <= this.dgInvoices.Rows.Count - 1)
            {
                new FormInvoiceInfo(this.dgInvoices.Rows[e.RowIndex].Cells[1].Value.ToString())
                {
                    MdiParent = base.MdiParent
                }.Show();
            }
        }

        private void FormOrderList_Load(object sender, EventArgs e)
        {
            this.dgInvoices.AutoResizeColumns();
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            base.Close();
        }
    }
}
